package com.naicheng.ivote.views.fragments;

import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.ConclusionDetail;
import com.naicheng.ivote.doman.OptionsDetial;
import com.naicheng.ivote.doman.SubtitleDetail;
import com.naicheng.ivote.doman.TestQuestion;
import com.naicheng.ivote.engin.BaseFragment;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.uiself.ToastUtils;
import com.naicheng.ivote.views.TestDetailQuestionActivity;
import com.naicheng.ivote.volley.Response.Listener;

public class StartTestFragment extends BaseFragment {

	@Override
	protected View initViews(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.test_start_question, null, false);
	}
	private TextView tv_question_title,tv_result;
	private ListView lv_options;
	
	private String requestcommand = "test";
	
	TestQuestion data;
	protected void init() {
		tv_question_title = getView(R.id.tv_question_title);
		tv_result = getView(R.id.tv_result);
		lv_options = getView(R.id.lv_options);
		
		if(ctx.getItem() != null){
			HashMap<String, String> m = new HashMap<String, String>();
			m.put("id", ctx.getItem().id+"");
			getDataFromServer(requestcommand , m , new Listener<JSONObject>() {
				@Override
				public void onResponse(JSONObject response) {
					BaseResponseEntity<TestQuestion> entity = new BaseParser<TestQuestion>().parserDomain(response, TestQuestion.class);
					if(entity != null && entity.getState() && entity.getData() != null){
						data = entity.getSingleDomain();
						List<SubtitleDetail> subtitle = data.getSubtitle();
						if(subtitle != null && subtitle.size() > 0){
							QuestionsAdapter adapter = new QuestionsAdapter(subtitle.get(0),true);
							lv_options.setAdapter(adapter);
						} else {
							ToastUtils.getNormalCenterToast(CTX, "暂时没有题目信息");
						}
					}else{
						ToastUtils.getNormalCenterToast(CTX, entity.getMsg());
					}
				}
			}, CTX);
		}
		
		lv_options.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				QuestionsAdapter adapter1 = (QuestionsAdapter) lv_options.getAdapter();
				OptionsDetial item = adapter1.getItem(position);
				if("1".equals(item.getType())){
					for (SubtitleDetail sd : data.getSubtitle()) {
						if(item.getSubtitleid().equals(String.valueOf(sd.getId()))){
							QuestionsAdapter adapter = new QuestionsAdapter(sd,false);
							lv_options.setAdapter(adapter);
							return;
						}
					}
				}else{
					if(data.getConclusions() != null && data.getConclusions().size() > 0){
						for (ConclusionDetail cd : data.getConclusions()) {
							if(item.getConclusionsequence().equals(String.valueOf(cd.getId()))){
								tv_result.setText("测试结果：\n"+cd.getText()+cd.getSequence()+"\n"+"（测试结果仅供娱乐 :-D ）");
								getView(R.id.ll_result).setVisibility(View.VISIBLE);
								return;
							}
						}
					}else{
						ToastUtils.getNormalCenterToast(CTX, "暂无答案信息");
					}
				}
			}
		});
		
		getView(R.id.bt_viewall).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ToastUtils.getNormalCenterToast(CTX, "查看全部");
			}
		});
		getView(R.id.bt_share).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ToastUtils.getNormalCenterToast(CTX, "分享");
			}
		});
	};
	
	class QuestionsAdapter extends BaseAdapter{
		SubtitleDetail subtitleDetail;
		private boolean isFirst;
		public QuestionsAdapter(SubtitleDetail subtitleDetail,boolean isFirst) {
			this.subtitleDetail = subtitleDetail;
			this.isFirst = isFirst;
			tv_question_title.setText(subtitleDetail.getTitle()+"\n"+subtitleDetail.getText());
		}

		@Override
		public int getCount() {
			return subtitleDetail.getOptions().size();
		}

		@Override
		public OptionsDetial getItem(int position) {
			return subtitleDetail.getOptions().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.item_question, null);
			TextView tv_option = (TextView) convertView.findViewById(R.id.tv_option);
			OptionsDetial item = getItem(position);
			tv_option.setText(item.getSequence()+item.getText());
			return convertView;
		}
	}
	
	TestDetailQuestionActivity ctx;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		ctx = (TestDetailQuestionActivity) activity;
	}
}
